package Database

import (
	"salarypayment-case/Model"
)

//go没有static，只能用全局变量代替
var PayrollDatabases = &PayrollDatabase{map[int]*Model.Employee{}, map[int]int{}}

type PayrollDatabase struct {
	itsEmployees    map[int]*Model.Employee
	itsUnionMembers map[int]int
}

func (pd *PayrollDatabase) clear() {
	pd.itsEmployees = map[int]*Model.Employee{}
	pd.itsUnionMembers = map[int]int{}
}

func (pd *PayrollDatabase) GetEmployee(empid int) *Model.Employee {
	if id, ok := pd.itsEmployees[empid]; ok {
		return pd.itsEmployees[id.GetEmpid()]
	}

	return nil
}

func (pd *PayrollDatabase) AddEmployee(empid int, e *Model.Employee) {
	pd.itsEmployees[empid] = e
}

func (pd *PayrollDatabase) DeleteEmployee(empid int) {
	delete(pd.itsEmployees,empid)
}

func (pd *PayrollDatabase) AddUnionMember(memberid int, e *Model.Employee) {
	empid := e.GetEmpid()
	pd.itsUnionMembers[memberid] = empid
}

func (pd *PayrollDatabase) GetUnionMember(memberid int) *Model.Employee {
	if id, ok := pd.itsUnionMembers[memberid] ; ok {
		return pd.itsEmployees[id]
	}

	return nil
}

func (pd *PayrollDatabase) RemoveUnionMember(memberid int) {
	delete(pd.itsUnionMembers,memberid)
}

func (pd *PayrollDatabase) GetAllEmployeeIds() []int {
	var list []int

	for keys := range pd.itsEmployees {
		list = append(list,keys)
	}

	return list
}